/**
 * Created by Brook on 2020/4/9.
 */

(function () {
    var async = require('async');
    var moment = require('moment');
    var sql_config = require('../../serverconfig.js');
    var model_sql = require('../../tool/db_helper.js').init(sql_config.Model_Config);
    var SQL_TXT = sql_config.SQL_TXT;
    var urlencode = require('urlencode');



    //接收请求 -  获取Gltf预处理任务 - 取任务
    exports.post_getgltftask = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //获取是否有未结束的预处理任务
            check_task:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT.check_gltf_data,[result.check_pars.appid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //获取任务
            get_data:['check_task', function (cb, result) {
                if (result.check_task.length ==0) {
                    model_sql.query(SQL_TXT.get_gltf_data_task,[],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    cb(null, result.check_task);
                };
            }],
            //判断是否有任务
            check_data:['get_data', function (cb, resulrt) {
                if (resulrt.get_data.length == 0) {
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 21,
                            "message": '中心列表无数据'
                        }
                    };
                    console.log("Response request data success ...")
                    res.json(data);
                    cb('中心列表中无数据')
                } else {
                    cb(null, [])
                };
            }],
            //绑定相关参数
            up_data:['check_data', function (cb, result) {
                if (result.check_task.length ==0) {
                    model_sql.query(SQL_TXT.up_gltf_data_task,[result.check_pars.appid,result.get_data[0].id],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    cb(null, []);
                };
            }],
            //回传数据
            response_data:['up_data', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": result.get_data
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    }

    //接收请求 -  预处理后的URl
    exports.post_gltfdata = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            check_data:['check_pars', function (cb,result) {
                model_sql.query(SQL_TXT.check_gltf_data_uuid,[result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            up_shvpath:['check_data', function (cb, result) {
                if (result.check_data.length == 0){
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 201,
                            "message": '无效的UUID'
                        }
                    };
                    res.json(data);
                    cb('uuid异常')
                } else {
                    model_sql.query(SQL_TXT.up_gltf_data_shvpath,[result.check_pars.shvpath, result.check_pars.uuid],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            response_data:['up_shvpath', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": "数据更新成功"
                    }
                };
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    }

    //接收请求 - Gltf预处理状态
    exports.post_gltfstate = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //获取uuid
            get_fileID:['check_pars', function (cb, result) {
                var {uuid} = result.check_pars;
                //查询数据
                model_sql.query(SQL_TXT.get_fileId_uuid, [uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //更新状态
            up_state:['get_fileID', function (cb, result) {
                if (result.get_fileID.length == 0) {
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 201,
                            "data": "无效的UUID"
                        }
                    };
                    res.json(data);
                } else {
                    var {uuid,parsingCode,parsingMessage,parsingType} = result.check_pars;
                    //查询数据
                    model_sql.query(SQL_TXT.up_gltf_state, [parsingType, parsingCode, parsingMessage, uuid],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            response_data:['up_state', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": "数据更新成功"
                    }
                };
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    }

    //获取Gltf列表
    exports.post_gltflist = function(req, res) {
        async.auto({
            get_data:function (cb){
                model_sql.query(SQL_TXT.get_gltf_data, [],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            },
            response_data:['get_data', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": result.get_data
                    }
                };
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    }

}).call(this);
